/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.stateless;

import entity.ProductionBatch;
import entity.PurchaseItem;
import entity.Shipping;
import entity.Warehouse;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Wx
 */
@Stateless
public class ShippingSessionBean implements ShippingSessionBeanLocal {

    @PersistenceContext
    private EntityManager entityManager;
    
    @Override
    public List<Shipping> getShippingOrders() {
        Query query = entityManager.createQuery("SELECT s FROM Shipping s");
        
        ArrayList<Shipping> shippingOrders = new ArrayList<Shipping>();
        
        for (Object o : query.getResultList()) {
            Shipping s = (Shipping) o;
            
            shippingOrders.add(s);
        }
        
        return shippingOrders;
    }
    
    @Override
    public void addShippingOrder(Shipping shippingOrder) {
        entityManager.persist(shippingOrder);
        entityManager.flush();
    }
    
    @Override
    public void updatePurchaseItem(Long purchaseItemId, Boolean isDelivered, String status) {
        PurchaseItem purchaseItem = entityManager.find(PurchaseItem.class, purchaseItemId);

        purchaseItem.setIsDelivered(isDelivered);
        purchaseItem.setStatus(status);

        entityManager.persist(purchaseItem);
        entityManager.flush();
    }
    
    @Override
    public void dequeueProductionBatchFromWH(Long warehouseId, Long productionBatchId) {
        Warehouse temp = entityManager.find(Warehouse.class, warehouseId);
        
        ProductionBatch temp3 = entityManager.find(ProductionBatch.class, productionBatchId);
        
        temp.getProductionBatches().remove(temp3);
        
        entityManager.persist(temp);
    }
}
